
********CONVEXITY********
use "$jfqa_rep/20_Stock_Setup.dta", clear

qui{
drop if missing(convexity_split)
drop if missing(futexcess)

gen recession=0
replace recession=1 if year==1970 & month<12
replace recession=1 if year==1973 & month>11
replace recession=1 if year==1974
replace recession=1 if year==1975 & month<4
replace recession=1 if year==1980 & month<8 & month>1
replace recession=1 if year==1981 & month>7
replace recession=1 if year==1982 & month<12
replace recession=1 if year==1990 & month>7
replace recession=1 if year==1991 & month<4
replace recession=1 if year==2001 & month>3 & month<12
replace recession=1 if year==2008
replace recession=1 if year==2009 & month<7
replace recession=1 if year==2020 & month==3
replace recession=1 if year==2020 & month==4

keep if recession==1

*convexity split rank
gen convex_five=.
sort monthyear convexity_split
by monthyear: replace convex_five=1 if _n<_N/5
by monthyear: replace convex_five=2 if _n>=_N/5 & _n<_N/5*2
by monthyear: replace convex_five=3 if _n>=_N/5*2 & _n<_N/5*3 
by monthyear: replace convex_five=4 if _n>=_N/5*3 & _n<_N/5*4
by monthyear: replace convex_five=5 if _n>=_N/5*4

replace futexcess=futexcess*100

egen vw_excess=wtmean(futexcess),  weight(mktcap) by(monthyear convex_five)

*consolidate to one observation per month
sort convex_five monthyear 
collapse vw_excess, by(convex_five monthyear)

sort  convex_five monthyear

keep if convex_five==1 | convex_five==5

sort monthyear convex_five

gen vw_excess_dif=vw_excess-vw_excess[_n+1] if convex_five==1

keep if convex_five==1

egen group=group(monthyear)
tsset group

egen std_vw=sd(vw_excess_dif)

egen mean_ret_vw=mean(vw_excess_dif) 

gen sharpe_vw=mean_ret_vw/std_vw 

egen skew_ret_vw=skew(vw_excess_dif)

egen kurt_ret_vw=kurt(vw_excess_dif)
}

newey vw_excess_dif , lag(4)

sum std_vw sharpe skew kurt


******ALL FACTORS*****
qui{
use "$jfqa_rep/FF5_Monthly_Factors.dta", clear

merge m:1 year month using "$jfqa_rep/FF_REV_UMD.dta"
drop if _merge==2
drop _merge 

merge m:1 year month using "$jfqa_rep/PS_Factors.dta"
drop if _merge==2
drop _merge

gen recession=0
replace recession=1 if year==1970 & month<12
replace recession=1 if year==1973 & month>11
replace recession=1 if year==1974
replace recession=1 if year==1975 & month<4
replace recession=1 if year==1980 & month<8 & month>1
replace recession=1 if year==1981 & month>7
replace recession=1 if year==1982 & month<12
replace recession=1 if year==1990 & month>7
replace recession=1 if year==1991 & month<4
replace recession=1 if year==2001 & month>3 & month<12
replace recession=1 if year==2008
replace recession=1 if year==2009 & month<7
replace recession=1 if year==2020 & month==3
replace recession=1 if year==2020 & month==4

keep if recession==1

egen group=group(monthyear)
tsset group
}

foreach i in mktrf smb hml rmw cma mom strev ltrev traded {

newey `i' , lag(4)

egen mean_ret_`i'=mean(`i') 

egen std_`i'=sd(`i')

gen sharpe_`i'=mean_ret_`i'/std_`i'

egen skew_`i'=skew(`i')

egen kurt_`i'=kurt(`i')

sum std_`i' sharpe_`i' skew_`i' kurt_`i'
}
